package android.pot.com.smartcar.tool;

import android.app.ProgressDialog;
import android.content.Context;
import android.pot.com.smartcar.R;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;


public class LoadingTool {

    private static PageLoadingInfo loadingInfo;

    public synchronized static void StartLoading(Context context) {
        try {
            if (loadingInfo != null) {
                loadingInfo.size++;
            } else {

                LayoutInflater inflater = LayoutInflater.from(context);
                // 得到加载view
                View view = inflater.inflate(R.layout.view_loading, null);
                // 加载布局
                LinearLayout layout = (LinearLayout) view.findViewById(R.id.dialog_view);
                ProgressDialog progressDialog = new ProgressDialog(context, R.style.CustomProgressDialog);
                progressDialog.show();
                // 设置布局
                progressDialog.setContentView(layout, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));


                PageLoadingInfo info = new PageLoadingInfo();
                info.size = 1;
                info.progressDialog = progressDialog;
                loadingInfo = info;
            }
        }catch (Exception e){

        }

    }

    public synchronized static void EndLoading(Context activity) {
        try {
            if (loadingInfo != null) {
                if (loadingInfo.size > 1) {
                    loadingInfo.size--;
                } else {
                    loadingInfo.progressDialog.dismiss();
                    loadingInfo = null;
                }
            }
        }catch (Exception e){

        }

    }

    static class PageLoadingInfo {
        int size;
        ProgressDialog progressDialog;
    }

}
